package com.itheima.demo;

import com.itheima.domain.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DemoTest1 {
    private SqlSession session = null;
    private InputStream is = null;

    @Before
    public void before() throws Exception {
        //读取核心配置文件
        is = Resources.getResourceAsStream("sql.xml");
        //构建SqlSessionFactory对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //获取SqlSession对象
        session = factory.openSession();
    }

    @After
    public void after() throws Exception {
        //事务提交
        session.commit();
        //释放资源
        session.close();
        is.close();
    }

    //查询所有
    @Test
    public void testFindAll() {
        //调用statement对象并且执行：namespace+"."+statement对象的id
        List<Student> list = session.selectList("one.findAll");
        System.out.println(list);
    }

    //根据id查询学生
    @Test
    public void testFindById() {
       Student student = session.selectOne("one.findById",2);
        System.out.println(student);
    }

    //统计学生数量
    @Test
    public void testFindCount() {
        Integer i = session.selectOne("one.findCount");
        System.out.println(i);
    }

    //统计男女数量
    @Test
    public void testFindSex() {
        List<Map> map = session.selectList("one.findSex");
        System.out.println(map);
        Map sexMap = new HashMap();
        for (Map map1 : map) {
            String sex = (String) map1.get("sex");
            Long total = (Long) map1.get("count(*)");
            sexMap.put(sex,total);
        }
        System.out.println(sexMap);
    }

    //修改测试
    @Test
    public void testUpdateById() {
        Student student = session.selectOne("one.findById", 1);
        student.setName("苏政");
        session.update("one.updateById",student);
    }

    //修改测试
    @Test
    public void testFindByNameAndAge(){
        //封装数据
        Map map=new HashMap();
        map.put("name","赵六");
        map.put("age","26");
        Student student = session.selectOne("one.findByNameAndAge",map);
        System.out.println(student);
    }

    //模糊查询测试
    @Test
    public void testFindLike(){
        List<Student> list = session.selectList("one.findLikeName", "%王%");
        //List<Student> list = session.selectList("one.findLikeName", "王");
        System.out.println(list);
    }

    //新增操作
    @Test
    public void testSave(){
        //封装user数据
        Student student=new Student();
        student.setName("周七");
        student.setSex("女");
        student.setAge(31);
        Date date = new Date();
        student.setBirthday(date);

        System.out.println("执行保存前："+student);
        session.insert("one.saveStudent",student);
        System.out.println("执行保存后："+student);
    }

}
